AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Use(activator)
	if self.LastUsed>CurTime() then 
		self.LastUsed = CurTime()+0.3
		return
	end
	self.LastUsed = CurTime()+0.3
	umsg.Start("gunfactorygui", activator)
	umsg.Short(self:EntIndex())
	umsg.Entity(self)
	umsg.End()
end

function ENT:StartProduction(ply,guntype)
	if guntype==0 then
		self.dt.Sparking = false
		timer.Destroy(tostring(self.Entity) .. "spawned_weapon")
		timer.Destroy(tostring(self.Entity) .. "dostuff")
		self.GunType = nil
		Notify(ply, 3, 3, "Gun production stopped.")
	else
		self.User = ply
		self.Time = gunfactoryweps[guntype][4]
		self.dt.Sparking = true
		timer.Create(tostring(self.Entity) .. "spawned_weapon", self.Time, 1, self.createGun, self)
		timer.Create(tostring(self.Entity) .. "dostuff", 60, self.Time/60, self.DoStuff, self)
		self.Ready = false
		self.GunType = guntype
		Notify(ply, 3, 3, "Producing a " .. gunfactoryweps[self.GunType][2])
	end
end

function ENT:DoStuff()
	local owner = self.User
	local productioncost = gunfactoryweps[self.GunType][3] / gunfactoryweps[self.GunType][4] * 60
	if !self.Entity:IsPowered() then
		Notify(owner, 4, 3, "Gun production failed due to low power.")
		self.dt.Sparking = false
		timer.Destroy(tostring(self.Entity) .. "dostuff")
		timer.Destroy(tostring(self.Entity) .. "spawned_weapon")
		return
	end
	if !owner:CanAfford(productioncost) then
		Notify(owner, 4, 3, "Gun production failed, not enough money.")
		self.dt.Sparking = false
		timer.Destroy(tostring(self.Entity) .. "dostuff")
		timer.Destroy(tostring(self.Entity) .. "spawned_weapon")
		return
	end
	owner:TakeMoney(math.ceil(productioncost))
	Notify(owner, 2, 3, "Used $"..tostring(productioncost).." creating a weapon in the gun factory")
end

function ENT:createGun()
	self.dt.Sparking = false
	local gun = ents.Create(gunfactoryweps[self.GunType][6])
	local trace = { }
		trace.start = self.Entity:GetPos()+self.Entity:GetAngles():Up()*15
		trace.endpos = trace.start + self.Entity:GetAngles():Forward()*-30 + self.Entity:GetAngles():Right()*-30 + self.Entity:GetAngles():Up()*60
		trace.filter = self.Entity
	local tr = util.TraceLine(trace)
	gun:SetPos(tr.HitPos)
	local ang = self.Entity:GetAngles()
	ang:RotateAroundAxis(ang:Up(), 90)
	gun:SetAngles(ang)
	gun:Spawn()
	Notify(self.User, 3, 3, "Gun Factory weapon production complete")
end

function ENT:OnRemove()
	timer.Destroy(tostring(self.Entity) .. "spawned_weapon")
	timer.Destroy(tostring(self.Entity) .. "dostuff")
end

function setgunfactoryweapon(ply, cmd, args)
	if (args[1]==nil || args[2] == nil) then return	end
	local guntype = tonumber(args[2])
	local factory = ents.GetByIndex(args[1])
	
	if (!ValidEntity(factory)) then return end	
	if (factory:GetClass()!="struct_gunfactory" || ply:GetPos():Distance(factory:GetPos())>300) then return end
	
	if (guntype==0 && ply != factory.dt.Owner) then 
		Notify(ply, 4, 3, "Only Gun Factory owner can cancel weapon production")
		return 
	end
	if (!factory:IsPowered()) then 
		Notify(ply, 4, 3, "Cannot make weapon, Gun Factory has low power")
		return 
	end
	if (factory.dt.Sparking && guntype != 0) then 
		Notify(ply, 4, 3, "Cannot make weapon, Gun Factory in use")
		return 
	end
	if (guntype != 0 && gunfactoryweps[guntype] == nil) then
		Notify(ply, 4, 3, "Cannot make invalid weapons")
		return 
	end
	if (guntype != 0 && factory.dt.Upgrade < gunfactoryweps[guntype][1]) then
		Notify(ply, 4, 3, "Cannot make invalid weapons")
		return 
	end
	factory:StartProduction(ply,guntype)
end
concommand.Add("setgunfactoryweapon", setgunfactoryweapon)